package com.tucc.hello.leetcode.no1_400.no1_200.no21_40.no36;

import cn.hutool.json.JSONUtil;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        char[][] board = solution.readData(TEST_DATA2);
        System.out.println(solution.isValidSudoku(board));
    }

    public boolean isValidSudoku(char[][] board) {
        Set<Character>[] set = new HashSet[9];
        Set<Character>[] set2 = new HashSet[9];
        Set<Character>[] set3 = new HashSet[9];
        for (int i = 0; i < 9; i++) {
            set[i] = new HashSet<>();
            set2[i] = new HashSet<>();
            set3[i] = new HashSet<>();
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                char ch = board[i][j];
                if (ch != '.') {
                    if (!set[i].add(ch) || !set2[j].add(ch)
                            || !set3[i / 3 * 3 + j / 3].add(ch)) {
                        return false;
                    }
                }

            }
        }
        return true;
    }


    public char[][] readData(String data) {
        List<String> stringList = JSONUtil.toList(data, String.class);
        char[][] chars = new char[stringList.size()][];
        for (int i = 0; i < chars.length; i++) {
            String string = stringList.get(i);
            string = string.substring(1, string.length() - 1).replaceAll("[\",]", "");
            chars[i] = string.toCharArray();
        }
        return chars;
    }

    public static String TEST_DATA1 =
            "[[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"]\n" +
                    ",[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"]\n" +
                    ",[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"]\n" +
                    ",[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"]\n" +
                    ",[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"]\n" +
                    ",[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"]\n" +
                    ",[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"]\n" +
                    ",[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"]\n" +
                    ",[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]";
    public static String TEST_DATA2 =
            "[[\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"]\n" +
            ",[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"]\n" +
            ",[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"]\n" +
            ",[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"]\n" +
            ",[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"]\n" +
            ",[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"]\n" +
            ",[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"]\n" +
            ",[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"]\n" +
            ",[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]";

}
